module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('tenants', {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      name: {
        type: Sequelize.STRING(255),
        allowNull: false,
      },
      subdomain: {
        type: Sequelize.STRING(63),
        allowNull: false,
        unique: true,
      },
      plan_id: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: 'plans',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
      },
      status: {
        type: Sequelize.ENUM('active', 'inactive', 'suspended', 'trial'),
        allowNull: false,
        defaultValue: 'trial',
      },
      config: {
        type: Sequelize.JSON,
        allowNull: true,
      },
      trial_ends_at: {
        type: Sequelize.DATE,
        allowNull: true,
      },
      subscription_ends_at: {
        type: Sequelize.DATE,
        allowNull: true,
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      deleted_at: {
        type: Sequelize.DATE,
        allowNull: true,
      },
    });

    await queryInterface.addIndex('tenants', ['subdomain']);
    await queryInterface.addIndex('tenants', ['status']);
    await queryInterface.addIndex('tenants', ['plan_id']);
  },

  down: async (queryInterface) => {
    await queryInterface.dropTable('tenants');
  },
};

